package com.taotao.manage.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.taotao.common.pojo.PicUploadResult;
import com.taotao.manage.service.PropertiesService;

@Controller
@RequestMapping("pic/upload")
public class PicUploadController {

	private static final List<String> EXTENSIONLIST = Arrays.asList("gif", "jpg", "jpeg", "png", "bmp");
	
	@Autowired
	private PropertiesService propertiesService;
	
	@PostMapping
	@ResponseBody
	public PicUploadResult uploadImg(@RequestParam("uploadFile") MultipartFile file) {
		PicUploadResult result = new PicUploadResult();
		result.setError(1);
		result.setMessage("图片不合法！");

		if (file == null) {
			return result;
		}

		try {
			// 校验后缀名
			String originalFilename = file.getOriginalFilename();
			String extension = StringUtils.substringAfterLast(originalFilename, ".");
			if (!EXTENSIONLIST.contains(extension)) {
				result.setMessage("图片格式不正！");
				return result;
			}

			// 验证内容
			BufferedImage image = ImageIO.read(file.getInputStream());
			if (image == null) {
				result.setMessage("图片内容不正！");
				return result;
			}

			result.setError(0);
			// 保存到服务器
			String filePath = getFilePath(originalFilename);
			file.transferTo(new File(filePath));

			// 响应
			String url = StringUtils.replace(
					StringUtils.replace(filePath, propertiesService.TAOTAO_IMAGE_FILE, propertiesService.TAOTAO_IMAGE_URL), "\\", "/");
			result.setUrl(url);
			result.setMessage(null);
			return result;
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
			result.setMessage(e.getMessage());
		}
		return result;
	}

	private String getFilePath(String sourceFileName) {
		String baseFolder = propertiesService.TAOTAO_IMAGE_FILE + File.separator + "images";
		Date nowDate = new Date();
		// yyyy/MM/dd
		String fileFolder = baseFolder + File.separator + new DateTime(nowDate).toString("yyyy") + File.separator
				+ new DateTime(nowDate).toString("MM") + File.separator + new DateTime(nowDate).toString("dd");
		File file = new File(fileFolder);
		if (!file.isDirectory()) {
			// 如果目录不存在，则创建目录
			file.mkdirs();
		}
		// 生成新的文件名
		String fileName = new DateTime(nowDate).toString("yyyyMMddhhmmssSSSS") + RandomUtils.nextInt(100, 9999) + "."
				+ StringUtils.substringAfterLast(sourceFileName, ".");
		return fileFolder + File.separator + fileName;
	}

}
